package com.zakler.action;


import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.zakler.po.Lesson;
import com.zakler.po.Student;
import com.zakler.po.Teacher;
import com.zakler.po.TempLesson;
import com.zakler.util.HibernateUtil;


@Results({
	@Result(name="listST_success",location="/addl.jsp",type=""),
	@Result(name="add_success",location="/index.jsp",type=""),
	@Result(name="listLS_success",location="/index.jsp",type="")
})
public class LessonAction extends BaseAction
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Teacher teacher;
	private Student student;
	private Lesson lesson;
	private TempLesson tempLesson;
	private List<Integer> listCheck;
	
	//listST
		@Action("lesson_listST")
		public String listST() throws Exception
		{

			Session session = HibernateUtil.putSession();
			Query query1 = session.createQuery("from Teacher");
			@SuppressWarnings("unchecked")
			List<Teacher> list1 = query1.list();
			Query query2 = session.createQuery("from Student");
			@SuppressWarnings("unchecked")
			List<Teacher> list2 = query2.list();
			session.close();
			getHttpServletRequest().getSession().setAttribute("Tlist", list1);
			getHttpServletRequest().getSession().setAttribute("Slist", list2);
			return "listST_success";
		}
		
	//add method
		@Action("lesson_add")
		public String add() throws Exception
		{
			Integer tid=tempLesson.getTid();
			String hms=tempLesson.toTime();
			System.out.println(tid);
			System.out.println(hms);
			System.out.println(listCheck.get(0));
			for(Integer i:listCheck)
			{
				Session session = HibernateUtil.putSession();
				Transaction transaction =session.beginTransaction();
				lesson.setTid(tid);
				lesson.setSid(i);
				//System.out.println(i);
				lesson.setTime(hms);
				session.save(lesson);
				transaction.commit();
				session.close();
			}
			
			return "add_success";
		}
	//search lesson and student by  teacher method	
		@Action("lesson_listLS")
		public String listLS() throws Exception
		{
			
				Session session = HibernateUtil.putSession();
				Query query=session.createQuery("");
				List<Lesson> lessons=query.list();
				session.close();
			
			
			return "listLS_success";
		}
		
	public Teacher getTeacher()
	{
		return teacher;
	}
	public void setTeacher(Teacher teacher)
	{
		this.teacher = teacher;
	}
	public Student getStudent()
	{
		return student;
	}
	public void setStudent(Student student)
	{
		this.student = student;
	}

	

	public List<Integer> getListCheck()
	{
		return listCheck;
	}

	public void setListCheck(List<Integer> listCheck)
	{
		this.listCheck = listCheck;
	}

	public TempLesson getTempLesson()
	{
		return tempLesson;
	}

	public void setTempLesson(TempLesson tempLesson)
	{
		this.tempLesson = tempLesson;
	}

	public Lesson getLesson()
	{
		return lesson;
	}

	public void setLesson(Lesson lesson)
	{
		this.lesson = lesson;
	}

	
	
}
